<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class HandmadesController extends AppController {

	public $name = 'Handmades';

public $uses = array('News','NewsCategory','Image');
	public $layout='default';
	public $mainCatId=3;

	public function index($cid=0) {
		$cid=intval($cid);
		
		$locale=Configure::read('Config.language');
		$cat=$this->NewsCategory->read(null,$cid);
		$cats=$this->NewsCategory->find('all',array('conditions'=>'NewsCategory.published=1 and NewsCategory.parent_id='.$this->mainCatId,'order'=>'NewsCategory.id desc'));
		if(!empty($cat)){
			$catname=$cat['NewsCategory']['title'];
			$conditions['News.parent_id']=$cid;
		}
		else{
			$catname=_resource('Made by yourself',true);
			foreach($cats as $k=>$v){
				$conditions['News.parent_id'][]=$v['NewsCategory']['id'];
			}
		}
		$conditions['NewsCategory.published']=1;
		$conditions['News.published']=1;
		$datas=$this->News->find('all',array('conditions'=>$conditions,'order'=>'important desc, News.id desc','limit'=>8));
		$this->set('datas',$datas);
		$this->set('cats',$cats);
		$this->set('catname',$catname);
		$this->set('locale',$locale);
		$this->set('cid',$cid);
		$this->set('css','handmade');
		
		//for seo
		$this->set('title_for_layout',$catname);
		$this->set('metakeyword',$catname);
		$this->set('metadescription',$catname);
		$this->set('ogTitle',$catname);
		$this->set('ogType','article');
		$this->set('ogDescription',$catname);
		
		
		
		//$this->redirect("homes/home");
	}
	public function detail($id=0) {	
		$locale=Configure::read('Config.language');
		$id=intval($id);
		$data=$this->News->find('first',array('conditions'=>'NewsCategory.published=1 and News.published=1 and News.id='.$id));
		if(!empty($data)){
			$numNews=3;
			//get next 5 
			$nextNews=$this->News->find('all',array('conditions'=>'News.published=1 and News.parent_id='.$data['News']['parent_id'].' and News.id>'.$id,'order'=>'News.id','limit'=>$numNews));
			//get prev 5 
			$prevNews=$this->News->find('all',array('conditions'=>'News.published=1 and News.parent_id='.$data['News']['parent_id'].' and News.id<'.$id,'order'=>'News.id desc','limit'=>$numNews));
			//check list of id
			$lsId=array($id);
			if(!empty($nextNews)){
				foreach($nextNews as $k=>$v){
					if(intval($v['News']['parent_id'])>0){
						//get parent name:
						$parent=$this->NewsCategory->findById(intval($v['News']['parent_id']));
						$parent=$this->NewsCategory->findById(intval($parent['NewsCategory']['parent_id']));
						if(!empty($parent))$nextNews[$k]['News']['controller']=$parent['NewsCategory']['title'];
					}	
					$lsId[]=$v['News']['id'];
				}
			}
			if(!empty($prevNews)){
				foreach($prevNews as $k=>$v){
					if(intval($v['News']['parent_id'])>0){
						//get parent name:
						$parent=$this->NewsCategory->findById(intval($v['News']['parent_id']));
						$parent=$this->NewsCategory->findById(intval($parent['NewsCategory']['parent_id']));
						if(!empty($parent))$prevNews[$k]['News']['controller']=$parent['NewsCategory']['title'];
					}
					$lsId[]=$v['News']['id'];
				}
			}
			//get news relate to this news:			
			$conditions['News.published']=1;
			$conditions['News.id NOT']=$lsId;
			$OR=array();
			if(!empty($data['News']['tag'])){
				foreach(explode(',',$data['News']['tag']) as $k=>$v){
					if(!empty($v)){						
						$OR['left(News.tag,LENGTH(\''.$v.'\')+1)']=$v.',';
						$OR['right(News.tag,LENGTH(\''.$v.'\')+1)']=','.$v;
						$OR['left(News.tag,LENGTH(News.tag)-LENGTH(\''.$v.'\')+LENGTH(\''.$v.'\')) like']='%,'.$v.',%';	
						$OR['Left(News.tag,LENGTH(\''.$v.'\'))']=$v;
					}
				}
				$conditions['OR']=$OR;
			}
			
			//$this->News->recursive=-1;
			
			$news=$this->News->find('all',array('conditions'=>$conditions,'order'=>'News.id desc','limit'=>$numNews));
			//get controller
			foreach($news as $k=>$v){
				if(intval($v['News']['parent_id'])>0){
					//get parent name:
					$parent=$this->NewsCategory->findById(intval($v['News']['parent_id']));
					$parent=$this->NewsCategory->findById(intval($parent['NewsCategory']['parent_id']));
					if(!empty($parent))$news[$k]['News']['controller']=$parent['NewsCategory']['title'];
				}	
			}			
			
			//get images relate
			unset($conditions);
			$conditions['ImageCategory.published']=1;

			$OR=array();
			if(!empty($data['News']['tag'])){
				foreach(explode(',',$data['News']['tag']) as $k=>$v){	
					if(!empty($v)){				
						$OR['left(Image.tag,LENGTH(\''.$v.'\')+1)']=$v.',';
						$OR['right(Image.tag,LENGTH(\''.$v.'\')+1)']=','.$v;
						$OR['left(Image.tag,LENGTH(Image.tag)-LENGTH(\''.$v.'\')+LENGTH(\''.$v.'\')) like']='%,'.$v.',%';	
						$OR['Left(Image.tag,LENGTH(\''.$v.'\'))']=$v;
					}
				}
				$conditions['OR']=$OR;
			}
			//t($conditions);
			$images=$this->Image->find('all',array('conditions'=>$conditions,'order'=>'Image.id desc','limit'=>8));
			
			$this->set('data',$data);
			$this->set('nextNews',$nextNews);
			$this->set('prevNews',$prevNews);
			$this->set('locale',$locale);
			$this->set('news',$news);
			$this->set('images',$images);
			$this->set('css','home,love');
			//for seo
			$this->set('metakeyword',$data['News']['title']);
			$this->set('metadescription',$data['News']['description']);
			$this->set('ogTitle',$data['News']['title']);
			$this->set('ogType','article');
			$this->set('ogDescription',$data['News']['description']);
			$this->set('ogUrl','/handmades/detail/'.$data['News']['id'].'/'.$locale);
			$this->set('ogImage',$data['News']['image']);
			$this->set('title_for_layout',$data['News']['title']);
			
			$this->render('/Loves/detail');
		}
		else{
			$this->index();
		}
		
		
	}
}
